package com.xidian.zhiancloud.controller.equipFacility.baseSetting;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.PreUnit;
import com.xidian.zhiancloud.qo.equipFacility.PreUnitQO;
import com.xidian.zhiancloud.qo.equipFacility.PuInAndOutQO;
import com.xidian.zhiancloud.service.equipFacility.PreUnitService;
import com.xidian.zhiancloud.vo.emergencyManagement.WarehousingVO;
import com.xidian.zhiancloud.vo.equipFacility.PreUnitVO;
import com.xidian.zhiancloud.vo.equipFacility.PuInAndOutVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 设备设施 基础设置 备品备件设置接口
 *
 * @Description: 备品备件设置
 * @Author: 任启亮
 * @Date: 2020/11/12
 */
@RestController
@RequestMapping("/PreUnit")
public class PreUnitController extends BaseController {
    @Autowired
    private PreUnitService preUnitService;

    /**
     * 增加备品备件设置
     *
     * @param preUnit 备品备件设置对象
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(PreUnit preUnit) {
        preUnit.setCompanyId(getSessionUserCompanyId());
        preUnit.setCreatorId(getSessionUserid());
        preUnit.setAddTime(new Date(System.currentTimeMillis()));
        Boolean result=preUnitService.save(preUnit);
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获取id获取备品备件设置
     *
     * @param id 备品备件设置对象的id
     * @return 备品备件设置对象
     */
    @GetMapping("/getById")
    public ResultDTO<PreUnit> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(preUnitService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除备品备件设置
     *
     * @param ids ids数组
     * @return 是否删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result=preUnitService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改备品备件设置
     *
     * @param preUnit 备品备件设置对象
     * @return 是否添加状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(PreUnit preUnit) {
        preUnit.setCompanyId(null);
        preUnit.setCreatorId(null);
        preUnit.setAddTime(null);
        Boolean result=preUnitService.updateById(preUnit);
        return ResultDTO.successOf(result,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查询
     *
     * @param preUnitQO 传递参数
     * @return 返回列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<PreUnitVO>> getListByQO(PreUnitQO preUnitQO) {
        if (preUnitQO.getCurrent()== null || preUnitQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        preUnitQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(preUnitService.getListByQO(preUnitQO), GeneralResponseEnums.SUCCESS);
    }
    /**
     * 获取出入库记录
     * @param id 出入库对象id
     * @return 出入库对象
     */
    @GetMapping("/getPuInAndOut")
    public ResultDTO<List<PuInAndOutVO>> getPuInAndOut(@RequestParam("id") Long id) {
        return ResultDTO.successOf(preUnitService.getPuInAndOut(id), GeneralResponseEnums.SUCCESS);
    }

}
